package com.apexsoft.aas.service.model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.Serializable;
import java.util.Map;
import java.util.Set;

/**
 * Class 下载请求包
 *
 * @author lowezheng
 * @version 1.0.0, 19/01/31
 */
@Deprecated
public class ADownloadRequest implements Serializable {
    /**
     * Field  数据包头部
     */
    private Map<String, String> heads;

    /**
     * Field  数据包体
     */
    private Map<String, Object> params;
    /**
     * Field  文件码
     */
    private String filecode;

    public Map<String, String> getHeads() {
        return heads;
    }

    public void setHeads(Map<String, String> heads) {
        this.heads = heads;
    }

    public Map<String, Object> getParams() {
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public String getFilecode() {
        return filecode;
    }

    public void setFilecode(String filecode) {
        this.filecode = filecode;
    }


    public static ADownloadRequest fromJson(JSONObject json) {
        ADownloadRequest req = new ADownloadRequest();
        if (json.containsKey("filecode")) {
            req.setFilecode(json.getString("filecode"));
        }
        if (json.containsKey("heads")) {
            JSONObject headJson = json.getJSONObject("heads");
            if (headJson != null) {
                req.setHeads(JSONObject.toJavaObject(headJson, Map.class));
            }
        }
        if (json.containsKey("params")) {
            JSONObject paramJson = json.getJSONObject("params");
            if (paramJson != null) {
                req.setParams(JSONObject.toJavaObject(paramJson, Map.class));
            }
        }
        return req;
    }


    /**
     * GRPC标准-JSON格式字符串，支持Any类型转换
     *
     * @return
     * @throws
     */
    public String toGRPCJsonString() {
        JSONObject json = (JSONObject) JSON.toJSON(this);
        if (this.heads != null) {
            Set<String> keys = heads.keySet();
            JSONObject headJson = new JSONObject();
            for (String key : keys) {
                headJson.put(key, ARequest.toGrpcAny(heads.get(key)));
            }
            json.remove("heads");
            json.put("head", headJson);
        }
        if (this.params != null) {
            Set<String> keys = params.keySet();
            JSONObject paramJson = new JSONObject();
            for (String key : keys) {
                paramJson.put(key, ARequest.toGrpcAny(params.get(key)));
            }
            json.remove("params");
            json.put("param", paramJson);
        }
        if (this.filecode != null) {
            json.put("filecode", filecode);
        }
        return json.toJSONString();
    }

    public <T> T getParamEx(String key, Class<T> cls) {
        if (params != null) {
            Object o = params.get(key);
            //防止JSONObject转成自己
            if (o == null) {
                return null;
            } else if (o instanceof JSONObject && cls != JSONObject.class) {
                return ((JSONObject) o).toJavaObject(cls);
            } else if (o.getClass() == cls) {
                return (T) o;
            } else {
                throw new RuntimeException("key value class: " + o.getClass().getName() + ", not match target class:" + cls.getName());
            }
        } else {
            return null;
        }

    }

}

